import unittest, requests


class XinWen(unittest.TestCase):
    def setUp(self) -> None:
        print('start case')

    def tearDown(self) -> None:
        print('end case')

    # 登录
    token = None
    userId = None

    def test_01_login(self):
        url = 'http://10.59.9.18/hotNews/user/v1/login'
        data = {'userName': '1234567890', 'passwd': '1234567890'}
        login = requests.post(url=url, json=data)
        print(login.json())
        # 提取 token、userId
        XinWen.userId = login.json()['result']['userId']
        XinWen.token = login.json()['result']['token']
        print(XinWen.userId)
        print(XinWen.token)

        # 根据频道查询视频列表

    columnId = None

    def test_02_findColumnList(self):
        url = 'http://10.59.9.18/hotNews/video/v1/findColumnList'
        ColumnList = requests.get(url=url)
        print(ColumnList.json())
        # 提取columnId
        XinWen.columnId = ColumnList.json()['result'][0]['columnId']
        print(XinWen.columnId)

    # 根据频道查询视频列表
    videoId = None
    authorId = None

    def test_03_findVideoList(self):
        url = 'http://10.59.9.18/hotNews/video/v1/findVideoList'
        header = {'userId': str(XinWen.userId), 'token': XinWen.token}
        data = {'columnId': XinWen.columnId, 'page': 1, 'size': 5}
        findVideoList = requests.get(url=url, headers=header, params=data)
        print(findVideoList.json())
        # 提取videoId
        XinWen.videoId = findVideoList.json()['result'][0]['videoId']
        print('videoId:', XinWen.videoId)
        # 提取authorId
        XinWen.authorId = findVideoList.json()['result'][0]['authorId']
        print('authorId:', XinWen.authorId)

    # 查询作者发布过的新闻
    # columnId = None

    def test_04_MyCollect(self):
        url = 'http://10.59.9.18/hotNews/video/v1/findVideoList'
        header = {'userId': str(XinWen.userId), 'token': XinWen.token}
        data = {'columnId': XinWen.columnId, 'page': 1, 'size': 5}
        MyCollect = requests.get(url=url, headers=header, params=data)
        print(MyCollect.json())

    # 点赞新闻资讯
    def test_05_MyCollect(self):
        url = 'http://10.59.9.18/hotNews/news/verify/v1/praiseNews'
        header = {'userId': str(XinWen.userId), 'token': XinWen.token}
        data = {'newsId': XinWen.videoId}
        MyCollect = requests.put(url=url, headers=header, data=data)
        print(MyCollect.json())


if __name__ == '__main__':
    unittest.main()
